
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import excel_info_sql
list1 = excel_info_sql.getConditions()
from biz import biz_mysql


def Inovel_test():
    for x in range(0, len(list1)):
        # 小说名字
        Novel_name = list1[x]["Novel_name"]
        # 日期
        insert_time = list1[x]["date_time"]
        # 章节
        Chapter = list1[x]["Chapter"]
        # 推广名称
        Extension_name = list1[x]["Extension_name"]
        # 平台名称
        Platform = list1[x]["Platform"]
        driver = webdriver.Chrome(r'D:\Program Files\python3.63\chromedriver.exe')
        # 进入 inovel.818tu.com
        driver.get("https://inovel.818tu.com/backend/notices")
        driver.maximize_window()
        driver.implicitly_wait(10)
        # time.sleep(2)
        if Platform == "掌中云1":
            username = ""
        elif Platform == "掌中云2":
            username = ""
        else:
            print("没有满足条件的平台")
            break
            # 定位账号
        driver.find_element_by_id('username').clear()
        driver.find_element_by_id('username').send_keys('{0}'.format(username))
        # 定位密码
        # time.sleep(0.5)
        driver.find_element_by_name('password').clear()
        driver.find_element_by_name('password').send_keys('{0}'.format(''))
        # 点击登录
        driver.find_element_by_id('loginbtn').click()

        # print(driver.title)
        # 获取页面cookie
        # cookies = driver.get_cookies()
        time.sleep(1)
        for m in range(3):
            try:
                WebDriverWait(driver,15).until(
                    EC.element_to_be_clickable((By.XPATH,"//button[text()='我知道了']"))
                )
                driver.find_element_by_xpath("//button[text()='我知道了']").click()
                # time.sleep(1)
            except Exception:
                print("未处理完弹出框问题")

        # 进入小说列表主页面
        try:
            WebDriverWait(driver, 15).until(
                EC.element_to_be_clickable((By.XPATH, '//*[@id="sidebar"]/ul/li[4]//span'))
            )
            # time.sleep(2)
            driver.find_element_by_xpath('//*[@id="sidebar"]/ul/li[4]//span').click()
        except Exception:
            print("点击小说列表主页面出错")

        #发送关键字搜索框
        try:
            WebDriverWait(driver, 15).until(
                EC.element_to_be_clickable((By.XPATH, '//div[1]/form//input[1]'))
            )
            driver.find_element_by_xpath('//div[1]/form//input[1]').clear()
            driver.find_element_by_xpath('//div[1]/form//input[1]').send_keys('{0}'.format(Novel_name))
        except Exception:
            print("发送关键字搜索框出错")

        # 点击搜索按钮
        try:
            WebDriverWait(driver, 15).until(
                EC.element_to_be_clickable((By.XPATH, '//form//button'))
            )
            driver.find_element_by_xpath('//form//button').click()
        except Exception:
            print("点击搜索按钮出错")
        #点击小说
        try:
            WebDriverWait(driver, 15).until(
                EC.element_to_be_clickable((By.XPATH, '//td[2]/a'))
            )
            driver.find_element_by_xpath('//td[2]/a').click()
        except Exception:
            print("点击小说出错")
        if Chapter == 1:
            #点击推广链接
            try:
                WebDriverWait(driver, 15).until(
                    EC.element_to_be_clickable((By.XPATH, '//li[{0}]/span[3]/a'.format(Chapter)))
                )
                driver.find_element_by_xpath('//li[{0}]/span[3]/a'.format(Chapter)).click()
            except Exception:
                print("点击推广链接出错")
            # 清理小说推广信息
            try:
                WebDriverWait(driver, 15).until(
                    EC.element_to_be_clickable((By.XPATH, '//*[@id="create-referral-link-modal"]//div[2]/div/input'))
                )
                driver.find_element_by_xpath('//*[@id="create-referral-link-modal"]//div[2]/div/input').clear()

            except Exception:
                print("清理小说推广信息出错")
            # 输入派单渠道名称
            try:
                driver.find_element_by_xpath('//*[@id="create-referral-link-modal"]//div[2]/div/input').send_keys('{0}'.format(insert_time+Extension_name))
            except Exception:
                print("输入派单渠道名称出错")
            # 选择渠道类型
            try:
                # time.sleep(1)
                WebDriverWait(driver, 15).until(
                    EC.element_to_be_clickable((By.XPATH, '//div[3]/div/label[2]/span'))
                )
                driver.find_element_by_xpath('//div[3]/div/label[2]/span').click()
            except Exception:
                print("选择渠道类型出错")

            # 点击生成推广链接
            try:
                WebDriverWait(driver, 15).until(
                    EC.element_to_be_clickable((By.XPATH, '//*[@id="create-referral-link-modal"]//div[3]/button'))
                )
                driver.find_element_by_xpath('//*[@id="create-referral-link-modal"]//div[3]/button').click()
            except Exception:
                print("点击生成推广链接出错")
            # 复制链接
            try:
                time.sleep(3)
                url = driver.find_element_by_xpath('//*[@id="get-referral-link-qrcode-modal"]//div[2]/div/div/div[1]/div[1]').text
                print(url)

                # 推广链接 ，平台名，书名，章节，时间，推广名称
                biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform,Novel_name =Novel_name,Chapter=Chapter,datetime=insert_time,Extension_name=Extension_name)
            except Exception:
                print("复制链接出错")

            # 点击取消
            try:
                time.sleep(0.5)
                driver.find_element_by_xpath('//*[@id="get-referral-link-qrcode-modal"]//button/span').click()
            except Exception:
                print("点击取消出错")
        else:
            #生成推广文案
            try:
                WebDriverWait(driver,15).until(
                    EC.element_to_be_clickable((By.XPATH,'//li[1]//div[1]//span'))
                )
                driver.find_element_by_xpath('//li[1]//div[1]//span').click()
            except Exception:
                print("生成推广文案出错")

            #图片模式
            try:
                WebDriverWait(driver,15).until(
                    EC.element_to_be_clickable((By.XPATH,'//li[1]//div[1]//li[2]/a'))
                )
                driver.find_element_by_xpath('//li[1]//div[1]//li[2]/a').click()
            except Exception:
                print("图片模式")

            #新页面

            # 获得打开的第一个窗口句柄
            window_1 = driver.current_window_handle
            # 获得打开的所有的窗口句柄
            windows = driver.window_handles
            # print(windows)
            # 切换到最新的窗口
            for current_window in windows:
                if current_window != window_1:
                    driver.switch_to.window(current_window)

            # 生成原文链接
            try:
                WebDriverWait(driver,15).until(
                    EC.element_to_be_clickable((By.XPATH,'//div[1]/span[1]/button'))
                )
                driver.find_element_by_xpath('//div[1]/span[1]/button').click()
            except Exception:
                print("生成原文链接")

            #渠道名
            try:
                time.sleep(1)
                driver.find_element_by_xpath('//form/div[2]//input').clear()
                driver.find_element_by_xpath('//form/div[2]//input').send_keys(insert_time+Extension_name)
            except Exception:
                print("渠道名")

            #点击外推
            try:
                WebDriverWait(driver,15).until(
                    EC.element_to_be_clickable((By.XPATH,'//span[text()="外推"]'))
                )
                driver.find_element_by_xpath('//span[text()="外推"]').click()
            except Exception:
                print("点击外推")

            #生成链接
            try:
                WebDriverWait(driver,15).until(
                    EC.element_to_be_clickable((By.XPATH,'//button[text()="生成链接"]'))
                )
                driver.find_element_by_xpath('//button[text()="生成链接"]').click()
            except Exception:
                print("生成链接")

            #原文链接
            try:
                WebDriverWait(driver,15).until(
                    EC.element_to_be_clickable((By.XPATH,'//div[2]/div/div/div[1]/div[1]'))
                )
                driver.find_element_by_xpath('//div[2]/div/div/div[1]/div[1]').click()
            except Exception:
                print("原文链接")

            # url
            try:
                time.sleep(2)
                url = driver.find_element_by_xpath('//div[2]/div/div/div[1]/div[1]').text
                print(url)
                # 推广链接 ，平台名，书名，章节，时间，推广名称
                biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform, Novel_name=Novel_name,Chapter=Chapter, datetime=insert_time, Extension_name=Extension_name)
            except Exception:
                print("获取url失败")

            #取消
            try:
                WebDriverWait(driver,15).until(
                    EC.element_to_be_clickable((By.XPATH,'//div[@id="get-referral-link-qrcode-modal"]//span[text()="×"]'))
                )
                driver.find_element_by_xpath('//div[@id="get-referral-link-qrcode-modal"]//span[text()="×"]').click()
            except Exception:
                print("取消")

            #二维码引导模板
            try:
                time.sleep(0.5)
                WebDriverWait(driver,15).until(
                    EC.element_to_be_clickable((By.XPATH,'//a[text()="二维码引导模板 "]'))
                )
                driver.find_element_by_xpath('//a[text()="二维码引导模板 "]').click()
            except Exception:
                print("二维码引导模板")

            #二维码选择
            try:
                WebDriverWait(driver,15).until(
                    EC.element_to_be_clickable((By.XPATH,'//li[6]//li[1]//img'))
                )
                driver.find_element_by_xpath('//li[6]//li[1]//img').click()
            except Exception:
                print("二维码选择")

            #复制
            try:
                time.sleep(0.5)
                WebDriverWait(driver,15).until(
                    EC.element_to_be_clickable((By.XPATH,'//a[text()=" 复制 "]'))
                )
                driver.find_element_by_xpath('//a[text()=" 复制 "]').click()
            except Exception:
                print("复制失败")

            #复制正文
            try:
                time.sleep(0.5)
                WebDriverWait(driver,15).until(
                    EC.element_to_be_clickable((By.XPATH,'//a[text()="复制正文"]'))
                )
                driver.find_element_by_xpath('//a[text()="复制正文"]').click()
            except Exception:
                print("复制正文失败")

            # time.sleep(1)
        driver.quit()

if __name__=="__main__":
    Inovel_test()